#!/bin/sh -x

FW=/Library/Frameworks/Mono.framework
FW_CURRENT=${FW}/Versions/Current
CURRENT=`basename $(readlink ${FW_CURRENT})`

# Remove PCL assemblies that we installed from Mono 3.1.1
LICENSE="Portable Class Library Reference Assemblies License-07JUN2013.docx"
if [ -f "$FW/External/xbuild-frameworks/.NETPortable/$LICENSE" ]; then
    echo "Removing PCL because we're upgrading from 3.1.1" >> /tmp/mono-installation
    rm -rf $FW/External/xbuild-frameworks/.NETPortable
fi

# Remove /usr/local/bin/pkg-config if it's a symlink to the Mono-installed one
PKG_CONFIG_LINK="/usr/local/bin/pkg-config"
if [ -L $PKG_CONFIG_LINK ]; then
    location=`readlink $PKG_CONFIG_LINK`
    case "$location" in
    *Mono.framework*) rm $PKG_CONFIG_LINK;;
    esac
fi

WHITELIST=$(cat "$(dirname "$0")/whitelist.txt")
MONO_COMMANDS_FILE=/etc/paths.d/mono-commands
FW_WHITELISTED_COMMANDS=${FW_CURRENT}/Commands

mkdir -p ${FW_WHITELISTED_COMMANDS}
mkdir -p $(dirname ${MONO_COMMANDS_FILE})

if test -e ${MONO_COMMANDS_FILE}; then
    rm "${MONO_COMMANDS_FILE}"
fi

echo "${FW_WHITELISTED_COMMANDS}" >> "${MONO_COMMANDS_FILE}"

if [ -d "${FW}"/Commands ]; then
    for i in ${WHITELIST}; do
        if test -e "${FW}/Commands/${i}"; then
            ln -s "${FW}/Commands/${i}" "${FW_WHITELISTED_COMMANDS}/${i}"
            #Cleanup any old symlinks in /usr/local/bin that we used to install
            rm -rf "/usr/local/bin/${i}"
        fi
    done;
    eval $(/usr/libexec/path_helper -s)
else
    echo "${FW}/Commands does not exist"
    echo "Can not add command links to $PATH."
fi

if [ -d ${FW_CURRENT} ]; then
    cd ${FW_CURRENT}/share/man
    for i in ${WHITELIST}; do
        for j in $(ls man*/${i}.*); do
            if test ! -e "/usr/local/share/man/${j}"; then
                ln -sf "${FW_CURRENT}/share/man/${j}" "/usr/local/share/man/${j}"
            fi
        done
    done

    cd ${FW_CURRENT}/etc
    # Make sure we run the files we lay down, and not other stuff installed on the system
    export PATH="${FW_CURRENT}/bin:$PATH"
    LOG_DIR=/Library/Logs/Mono.framework
    mkdir ${LOG_DIR}

    echo ======= `date` ===== >> "${LOG_DIR}/postinstall-gdk-pixbuf-query-loaders.log"
    echo ======= `date` ===== >> "${LOG_DIR}/postinstall-pango-querymodules.log"
    echo ======= `date` ===== >> "${LOG_DIR}/postinstall-fc-cache.log"
    echo ======= `date` ===== >> "${LOG_DIR}/gtk-query-immodules-2.0.log"

    # gtk+ setup
    gdk-pixbuf-query-loaders --update-cache >> "${LOG_DIR}/postinstall-gdk-pixbuf-query-loaders.log" 2>&1 || true
    # pango setup
    mkdir -p pango
    pango-querymodules > pango/pango.modules 2>> "${LOG_DIR}/postinstall-pango-querymodules.log" || true
    pango-querymodules --update-cache >> "${LOG_DIR}/postinstall-pango-querymodules.log" 2>&1 || true
    fc-cache -v >> "${LOG_DIR}/postinstall-fc-cache.log" 2>&1 || true

    cd ${FW_CURRENT}/lib/gtk-2.0/2.10.0
    gtk-query-immodules-2.0 > immodules.cache 2>> "${LOG_DIR}/gtk-query-immodules-2.0.log" || true
fi

# Delete older Monos
#
# - keep if the major version is different
# - keep if 'Versions/x.y.z/keep' exists
# - Keep if it is greater than $CURRENT
#
echo "Current:" $CURRENT >> /tmp/mono-installation

pushd ${FW}/Versions
for i in `ls -d *`; do
    result=`echo "${i:0:1} == ${CURRENT:0:1}" | bc`
    if [ $result -ne 1 ]; then
        echo "keeping" $i "because it has a different major version" >> /tmp/mono-installation
        continue
    fi

    if [ -f $i/keep ]; then
        echo "Keeping" $i "because of keep file" >> /tmp/mono-installation
        continue
    fi

    # A magical bit of Perl: http://stackoverflow.com/a/7366753/494990
    result=$(perl -e '($a,$b)=@ARGV; for ($a,$b) {s/(\d+)/sprintf "%5d", $1/ge}; print $a cmp $b;' $i $CURRENT)
    if [ $result -ge 0 ]; then
        echo "Skipping" $i "because $i >= $CURRENT" >> /tmp/mono-installation
        continue

    else
        echo "rm -rf" $i >> /tmp/mono-installation
        rm -rf $i
    fi
done
popd

# Mono framework should be owned by root
chown -R root:admin ${FW}
